#include <iostream>
#include <algorithm>
#define maxn 1000
using namespace std;
int T, M, dp[maxn][maxn], cost[maxn], val[maxn];
int f[maxn];
int main()
{

    cin >> T >> M;
    for (int i = 1; i <= M; ++i)
    {
        cin >> cost[i] >> val[i];
    }
    for (int i = 1; i <= M; ++i)
    {
        for (int j = 0; j <= T; ++j)
        {
            if (j >= cost[i])
                dp[i][j] = max(dp[i - 1][j], dp[i][j - cost[i]] + val[i]);
            else
                dp[i][j] = dp[i - 1][j];
        }
    }
    cout << dp[M][T];
    return 0;
}